/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Application;

import Maps.GoogleMaps;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

/**
 *
 * @author Stanley
 */
public class MapTypes extends List implements CommandListener{
    private MainMidlet midlet;
    private Command back;
    private Command select;
    
    public MapTypes(MainMidlet midlet) {
        super("Map types", List.EXCLUSIVE);
        // command
        back = new Command("Back", Command.BACK, 1);
        select = new Command("Select", Command.SCREEN, 1);
        // map type options
        append("Road map", null);
        append("Satellite", null);
        append("Terrain", null);
        append("Hybrid", null);
        this.midlet = midlet;
        
        // add command
        setCommandListener(this);
        addCommand(back);
        addCommand(select);
        setSelectCommand(select);
    }
    
    public void commandAction(Command c, Displayable d) {
        if(c == back)
            midlet.optionsShow();
        else if (c == select) {
            // get information from Home screen
            double[] lanLng = midlet.getHome().getLanLng();
            int zoom = midlet.getHome().getZoomLevel();
            // new google maps
            GoogleMaps maps = new GoogleMaps();
            Image map = null;
            
            List list = (List) d;
            int selectedIdx = list.getSelectedIndex();
            try {
                switch (selectedIdx) {
                    case 0: // load road map
                        midlet.getHome().setMaptype(MapType.DEFAULT);
                        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoom, "png32", MapType.DEFAULT);
                        break;
                    case 1: // load satellite map
                        midlet.getHome().setMaptype(MapType.SATELLITE);
                        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoom, "png32", MapType.SATELLITE);
                        break;
                    case 2: // load terrain map
                        midlet.getHome().setMaptype(MapType.TERRAIN);
                        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoom, "png32", MapType.TERRAIN);
                        break;
                    case 3: // load hybrid map
                        midlet.getHome().setMaptype(MapType.HYBRID);
                        map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], zoom, "png32", MapType.HYBRID);
                        break;
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
            
            // set map and show home screen
            midlet.getHome().setMap(map);
            midlet.homeShow();
        }
    }
}
